/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TweetRay;

import Raytracing.Shaders.Material;
import Raytracing.Shaders.Shader;
import Raytracing.Geometry.Triangle;
import java.util.ArrayList;
import processing.core.PApplet;
import processing.core.PGraphics3D;
import processing.core.PVector;

/**
 *
 *           __
 *          /  \
 *          ^  ^
 *         / \/ \
 *         | .| .|
 *         \_/\_/
 *          |  |
 *          \ o/
 *          /  \
 *         |    |
 *         | | ||
 *         | 0 0|
 *         |    |
 */
public class GroundPlane extends Entity implements Traceable {

    private float height = -255;
    private float extent = 255;


    int visible;

    public GroundPlane(Material[] shaders, int shaderIndex) {
        visible = 1;
        height = 255;
        extent = 512;
        this.shaders = shaders;
        this.shaderIndex = shaderIndex;
    }

    public void draw(PGraphics3D p3d) {

        if (visible == 1) {
            p3d.pushMatrix();

            p3d.translate(128, height, 128);
            p3d.rotateX(PApplet.PI / 2);

            p3d.fill(128, 128, 128,255);
            p3d.rect(-extent / 2, -extent / 2, extent, extent);

            p3d.popMatrix();
        }
    }

    public ArrayList tesselate() {
        ArrayList toRet = new ArrayList();
        Triangle tri1 = new Triangle((Shader)shaders[shaderIndex],
                new PVector(extent / 2 + 128, height, -extent / 2 + 128),
                new PVector(-extent / 2 + 128, height, -extent / 2 + 128),
                new PVector(-extent / 2 + 128, height, extent / 2 + 128),
                0, 0, 0, 1, 1, 1, null);


        Triangle tri2 = new Triangle((Shader)shaders[shaderIndex],
                new PVector(-extent / 2 + 128, height, extent / 2 + 128),
                new PVector(extent / 2 + 128, height, extent / 2 + 128),
                new PVector(extent / 2 + 128, height, -extent / 2 + 128),
                1, 1, 1, 0, 0, 0, null);

        tri1.setUVScale(5, 5);
        tri2.setUVScale(5, 5);
        toRet.add(tri1);
        toRet.add(tri2);




        return toRet;
    }

    @Override
    public void pack(UTFWriter output) {
        //System.out.println("Exporting groundplane");

        //output.writeBits((int) visible, 1);
        //output.writeBits(shaderIndex, 4);
    }

    @Override
    public void scale(int axis, float amt) {
    }

    @Override
    public Entity copy() {
        return null;
    }
    public void setShader(int shaderIndex) {

        this.shaderIndex = shaderIndex;
        tesselate();

    }
    public void pickDraw(PGraphics3D p3d, int index) {
    }
}
